package com.hxzon.aixzsecretary;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;

import org.dom4j.Element;

import com.hxzon.aixzsecretary.xml.AixzMessageProcessor;

public class SecretaryTimerTask extends TimerTask {

	@Override
	public void run() {
		AixzMessageProcessor xml = AixzMessageProcessor.getInstance();
		xml.openXML();
		String date = new Date().toString();
		Iterator messages = xml.getMessages("20090508");
		Timer timer = new Timer();
		if (messages != null) {
			while (messages.hasNext()) {
				Element message = (Element) messages.next();
				final String title = xml.getMessageDetail(message, "title");
				final String content = xml.getMessageDetail(message, "content");
				final String time = xml.getMessageDetail(message, "time");
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss");
				Date time2 = null;
				try {
					time2 = df.parse(time);
					time2.setHours(time2.getHours() - 2);
				} catch (ParseException e) {
					System.out.println(e);
				}

				timer.schedule(new TimerTask() {
					public void run() {
						JOptionPane.showMessageDialog(null, "在" + time
								+ " 时您有以下任务要完成：\n " + content, title, 1);

						cancel();
					}
				}, time2);

			}

		}
		System.out.println("start timertask");
	}

}
